{% extends "layout_backend.html" %}

{% block header %}
<h1>Amdin Panel - Master</h1>
{% end %}

{% block script %}
<script type="text/javascript">
  var admin = Object();
  
  admin.trtag_node = null;
  admin.trtag_text = '';

  admin.showtags=function(idx){
  $("#itembox_"+idx).toggle();
  };

  admin.addtag = function(idx){
  tag_name = $.trim($("#tag_"+idx).val())
  if(tag_name == ""){
  alert("您添加的标签怎么能空呢？（全角/半角 空格都是不允许的！）");
  }else{
  var d = {tagname:tag_name,type:idx};
  $.post('/admin/tags/add',d,function(data){
  var o = $.parseJSON(data);
  var aid = parseInt(o.code);
  if(aid>0){
  d.id = aid;
  $("#itembox_"+idx).append("<span class='t'><a onclick='admin.trtag(this,"+aid+")'>"+d.tagname+"</a></span> ")
  }
  alert(o.msg);
  if(aid==0){
  location.reload();
  }
  });
  }
  };

  admin.trtag=function(node,tid){
  if(admin.trtag_node!=null){
  $(admin.trtag_node).html(admin.trtag_text);
  }
  admin.trtag_node = $(node).parent();
  admin.trtag_text = $(node).parent().html();
  var html = '<input type="text" id="cinput" name="tagname" value="'+$(node).text()+'" style="width:100px" /> <input type="button" value="apply" onclick="admin.applytag('+tid+')"/> <input type="button" value="trash" onclick="admin.trashtag('+tid+')"/>';
  $(node).parent().html(html);
};

  admin.applytag=function(tid){
  var tname = $("#cinput").val();
  var d = {tagname:tname,id:tid};
  $.post('/admin/tags/apply',d,function(data){
  admin.trtag_text = "<a onclick='admin.trtag(this,"+d.id+")'>"+d.tagname+"</a>";
  $(admin.trtag_node).html(admin.trtag_text);
  });
  };
  
  admin.trashtag=function(tid){
  if(window.confirm("点击确定放入回收站!")){
  $.post('/admin/tags/trash',{id:tid},function(data){
  $(admin.trtag_node).fadeOut("slow",function(){$(this).remove()});
  admin.trtag_node = null;
  });
  }
  }
</script>

{% end %}
		
{% block nav %}
<nav>
  <a href="/admin/business">商家</a>
  <a href="/admin/users">会员</a>
  <a href="/admin/tags">标签</a>
  <a href="/logout">退出</a>
</nav>
{% end %}

{% block content %}
<div class="tag">
  <a href="/admin/tags/all">展开</a>
</div>

{% for idx,iname in enumerate(val['tags_type']) %}
<div class="item">
  <span class="btn" onclick="admin.showtags({{ idx }})"><h2>{{ iname }}<sub><img width="32" src="/static/open.png" /></sub></h2></span>
  <div id="itembox_{{ idx }}" class="itembox">
	<div class="bar">
	  <input type="text" id="tag_{{ idx }}" name="tag_name" value="" />
	  <input type="button" value="添加标签至 [ {{ iname }} ]" onclick="admin.addtag({{ idx }})"/>
	</div>
	{% for tag in [ii for ii in val['tags_set'] if ii.type==idx and ii.delete==0] %}
	<span class="t" ><a onclick="admin.trtag(this,{{ tag.id }})">{{ tag.name }}</a></span>
	{% end %}
  </div>
</div>
{% end %}

<div class="item">
  <span class="btn" onclick="admin.showtags('trash')"><h2>回收站<sub><img width="32" src="/static/open.png" /></sub></h2></span>
  <div id="itembox_trash" class="itembox">
	{% for tag in [ii for ii in val['tags_set'] if ii.delete==1] %}
	<span class="t" ><a onclick="admin.reset(this,{{ tag.id }})">{{ val['tags_type'][tag.type] }} : {{ tag.name }}</a></span>
	{% end %}
  </div>
</div>

{% end %}


